package ${basePackage};

import ${servicePackage}.${className}Service;
import ${beanPackage}.${className};
import gz.util.R;
import gz.util.BeanMapUtil;

import java.util.*;
import java.util.Map;

import gz.util.ReqUtil;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;


@Api(tags = "${cname}")
@Controller
@RequestMapping("/${humpName}")
public class ${className}Controller {

    private static final Logger logger = LoggerFactory.getLogger(${className}Controller.class);

    @Autowired
    private ${className}Service ${humpName}Service;

    @ApiOperation("跳转后台管理分页面")
    @GetMapping("/pages/list")
    public String page() {
    	logger.info("in>>");
    	return "${humpName}/list";
    }

    @ApiOperation("跳转后台管理新增页面")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键编号,新增id传空"),
    }
    )
    @GetMapping("/pages/add")
    public String page_add(Model model, String id) {
        logger.info("in>>");
        ${className} ${humpName} = new ${className}();
        if(StringUtils.isNotEmpty(id)){
            ${humpName} = ${humpName}Service.getById(id);
        }
        model.addAttribute("${humpName}",${humpName});
        return "${humpName}/edit";
    }

    @ApiOperation("跳转微信展示页更新点击量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键编号,新增id传空"),
    }
    )
    @GetMapping("/pages/view")
    public String page_view(Model model, String id) {
        logger.info("in>>");
        ${className} ${humpName} = new ${className}();
        if(StringUtils.isNotEmpty(id)){
            ${humpName} = ${humpName}Service.getById(id);
        }
        model.addAttribute("${humpName}",${humpName});
        return "${humpName}/edit";
    }

    @ApiOperation("新增提交")
    @ApiResponses({ @ApiResponse(code = 200, message = "操作成功")})
    @PostMapping("/save")
    @ResponseBody
    public R save(HttpServletRequest request,${className} ${humpName}) {
        String token = ReqUtil.getToken(request);
        if(StringUtils.isEmpty(token)){
            return R.error(196,"未登录");
        }
    	return ${humpName}Service.save(token,${humpName});
    }

    @ApiOperation("编辑提交")
    @ApiResponses({ @ApiResponse(code = 200, message = "操作成功")})
    @PostMapping("/update")
    @ResponseBody
    public R update(HttpServletRequest request,${className} ${humpName}) {
        String token = ReqUtil.getToken(request);
        if(StringUtils.isEmpty(token)){
            return R.error(196,"未登录");
        }
        return ${humpName}Service.update(token,${humpName});
    }

    @ApiOperation("单条删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键编号"),
    }
    )
    @ApiResponses({ @ApiResponse(code = 200, message = "操作成功")})
    @PostMapping("/delete")
    @ResponseBody
    public R delete(${className} ${humpName}) {
    	return ${humpName}Service.delete(${humpName});
    }

    @ApiOperation("批量删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键编号,多个用逗号分割"),
    }
    )
    @ApiResponses({ @ApiResponse(code = 200, message = "操作成功")})
    @PostMapping("/deletes")
    @ResponseBody
    public R deletes(String id) {
    	if(StringUtils.isEmpty(id)) {
    		return R.error(198,"参数获取失败");
    	}
    	return ${humpName}Service.deletes(id);
    }

    @ApiOperation("根据编号查询信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键编号"),
    }
    )
    @ApiResponses({ @ApiResponse(code = 200, message = "操作成功")})
    @GetMapping("/load")
    @ResponseBody
    public R load(String id) {
    	if(StringUtils.isEmpty(id)) {
    		return R.error(198,"参数获取失败");
    	}
    	return ${humpName}Service.load(id);
    }

    @ApiOperation("分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "起始行"),
            @ApiImplicitParam(name = "rows", value = "每页行数"),
    }
    )
    @ApiResponses({ @ApiResponse(code = 200, message = "操作成功")})
    @PostMapping("/pagging")
    @ResponseBody
    public R pagging(Integer page,Integer rows,${className} ${humpName}) {
     	logger.info("进入");
        if(page == null || page.intValue() < 0){
            page = 1;
        }
        if(rows == null || rows.intValue() < 0){
            rows = 1;
        }
     	PageHelper.startPage(page, rows);
        try {
            PageInfo pageInfo = new PageInfo(${humpName}Service.list(${humpName}));
            return R.ok("查询成功").put("totalPage", pageInfo.getPages()).put("total",pageInfo.getTotal()).put("rows",pageInfo.getList());
        } catch (Exception e) {
            logger.error("程序异常",e);
            return R.ok(0,"程序异常").put("totalPage", 0).put("total",0).put("rows",new ArrayList());
        }
    }

    @ApiOperation("综合分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "起始行"),
            @ApiImplicitParam(name = "rows", value = "每页行数"),
    }
    )
    @ApiResponses({ @ApiResponse(code = 200, message = "操作成功")})
    @PostMapping("/PaggingMap")
    @ResponseBody
    public R PaggingMap(Integer page,Integer rows,${className} ${humpName}) {
     	logger.info("进入");
        if(page == null || page.intValue() < 0){
            page = 1;
        }
        if(rows == null || rows.intValue() < 0){
            rows = 1;
        }
     	PageHelper.startPage(page, rows);
        try {
            PageInfo pageInfo = new PageInfo(${humpName}Service.listMap(BeanMapUtil.beanToMap(${humpName})));
            return R.ok("查询成功").put("totalPage", pageInfo.getPages()).put("total",pageInfo.getTotal()).put("rows",pageInfo.getList());
        } catch (Exception e) {
            logger.error("程序异常",e);
            return R.ok(0,"程序异常").put("totalPage", 0).put("total",0).put("rows",new ArrayList());
        }
    }

}